package yja.gzw.app.activity

import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import yja.gzw.app.R
import yja.gzw.app.adapter.JingpingAdaptzer
import yja.gzw.app.adapter.PinglongAdapter
import yja.gzw.app.adapter.TuijianAdapter
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityDuntaixiangqingBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.Commentpl
import yja.gzw.app.viewmodel.Mainviewmodel
import com.google.android.material.bottomsheet.BottomSheetDialog

class DuntaixiangqingActivity :BaseActivity<Mainviewmodel, ActivityDuntaixiangqingBinding>(R.layout.activity_duntaixiangqing), PinglongAdapter.OnItemClickListener{
    private lateinit var fabiada: PinglongAdapter
    private lateinit var bottomSheetDialog: BottomSheetDialog
    var id:String=""
    var xiaobiao=0
    var idid=""
    var shoucangzhuangtai=""
    var shoucangshu=0
    var dianzanshu=0
    var dianzauangtai=""
    override fun initView() {

        binding.caidan.setOnClickListener {
            bottomSheetDialog = BottomSheetDialog(this)
            val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_jubao, null)
            bottomSheetDialog.setContentView(view)
            val chacha =view.findViewById<ImageView>(R.id.chacha)
            val jubaoh =view.findViewById<ImageView>(R.id.jubaohong)
            chacha.setOnClickListener {
                bottomSheetDialog.dismiss()
            }
            jubaoh.setOnClickListener {
                val inten=Intent(this, JubaoActivity::class.java)
                startActivity(inten)

            }
            bottomSheetDialog.show()

        }
        binding.fanhui.setOnClickListener {
            finish()
        }
        binding.guanzhu.setOnClickListener {
            viewmodel.onguanzhuhequguanwang(UserData.token, mapOf("user_id" to idid))
        }


        binding.fengxiangpengyouquan.setOnClickListener {
            bottomSheetDialog = BottomSheetDialog(this)
            val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_fenxiang, null)
            bottomSheetDialog.setContentView(view)

            val cha=view.findViewById<ImageView>(R.id.chacha)
                cha.setOnClickListener {
                    bottomSheetDialog.dismiss()
                }

            bottomSheetDialog.show()

        }


        id=intent.getStringExtra("forumid")!!
        viewmodel.ondongtaixiangqingwang(UserData.token, mapOf("forum_id" to id))
            viewmodel.onpinglongwang(UserData.token, mapOf("forum_id" to id))

        binding.shoucang.setOnClickListener {


            viewmodel.onshoucangwang(UserData.token, mapOf("forum_id" to id))

        }

        binding.dianzan.setOnClickListener {

               viewmodel.ondianzanwang(UserData.token,mapOf("forum_id" to id))
        }


        binding.pinglongfu.setOnClickListener {
            bottomSheetDialog = BottomSheetDialog(this)
            val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_fasongpinglun, null)
            bottomSheetDialog.setContentView(view)
            val shurukuang =view.findViewById<TextView>(R.id.shurukuang)
            val queding =view.findViewById<TextView>(R.id.queding)
            val quxiao =view.findViewById<TextView>(R.id.quxiao)
            val chacha =view.findViewById<ImageView>(R.id.chacha)
            chacha.setOnClickListener {
                bottomSheetDialog.dismiss()
            }
            quxiao.setOnClickListener {
                bottomSheetDialog.dismiss()
            }
            queding.setOnClickListener {
                viewmodel.onfabupinglongwang(UserData.token, mapOf("forum_id" to id,"pid" to "0","content" to shurukuang.text.toString(),"level" to "1","reply_id" to ""))



            }

            bottomSheetDialog.show()


        }




        binding.touxiang.setOnClickListener {
            val intent=Intent(this,HaoyouzhuyeActivity::class.java)
            //val intent=Intent(this,CeshiActivity::class.java)
            intent.putExtra("id",id)
            startActivity(intent)

        }


    }

    override fun dataObservese() {

        viewmodel.ondianzan.observe(this,Observer{
            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                if(dianzauangtai=="1"){
                    dianzauangtai="0"
                    binding.dianzan.setImageResource(R.mipmap.dianzan)
                    binding.shuzi4.text=(dianzanshu-1).toString()
                    dianzanshu--
                }else{
                    dianzauangtai="1"
                    binding.shuzi4.text=(dianzanshu+1).toString()
                    binding.dianzan.setImageResource(R.mipmap.dianzanhou)
                    dianzanshu++
                }

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })

        viewmodel.onguanzhuhequguan.observe(this, Observer {

            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }else{

                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }


        })


        viewmodel.onshoucang.observe(this, Observer {
            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                if(shoucangzhuangtai=="1"){
                    shoucangzhuangtai="0"
                    binding.shoucang.setImageResource(R.mipmap.shoucangyuan)
                    binding.shuzi2.text=(shoucangshu-1).toString()
                    shoucangshu--
                }else{
                    shoucangzhuangtai="1"

                    binding.shuzi2.text=(shoucangshu+1).toString()
                    shoucangshu++
                    binding.shoucang.setImageResource(R.mipmap.shoucangle)

                }



            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }


        })


        viewmodel.ondianzanpl.observe(this, Observer{
            if(it.code==1){
                viewmodel.onpinglongwang(UserData.token, mapOf("forum_id" to id))

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()


            }

        })



    viewmodel.onfabupinglong.observe(this, Observer {
        if(it.code==1){
            bottomSheetDialog.dismiss()
            viewmodel.onpinglongwang(UserData.token, mapOf("forum_id" to id))
            Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
        }


    })

        viewmodel.onpinglong.observe(this, Observer {
            if(it.code==1){
                Log.e("评论数据",it.data.data.toString())

                binding.pinglong.text="评论（${it.data.total}）"
                binding.plreview.layoutManager=
                    LinearLayoutManager(this)
                fabiada= PinglongAdapter(this,it.data.data,this)
                binding.plreview.adapter= fabiada
                binding.plreview.scrollToPosition(xiaobiao)


            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }

        })

        viewmodel.ondongtaixiangqing.observe(this, Observer {
            if(it.code==1){

                Log.e("详情数据",it.data.toString())
                val imgs=it.data.images.split(",")

                if(it.data.is_collection_count==1){

                    shoucangzhuangtai="1"
                    binding.shoucang.setImageResource(R.mipmap.shoucangle)
                }else{
                    shoucangzhuangtai="0"
                    binding.shoucang.setImageResource(R.mipmap.shoucangyuan)


                }

                if(it.data.is_fav_count==1){

                    dianzauangtai="1"
                    binding.dianzan.setImageResource(R.mipmap.dianzanhou)
                }else{
                    dianzauangtai="0"
                    binding.dianzan.setImageResource(R.mipmap.dianzan)


                }


                binding.xbn.setData(imgs,null)
                binding.xbn.setmAutoPalyTime(3000)
                binding.xbn.setmAutoPlayAble(true)
                binding.xbn.setmAdapter { banner, view, position ->

                    Glide.with(banner.context).load(imgs[position]).into(view as ImageView)


                }

                binding.didian.text=it.data.address
                    binding.biaoti.text=it.data.title
                binding.neirong.text=it.data.content
                val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
                Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.user.avatar)
                    .apply(requestOptions)
                    .into(binding.touxiang)
                binding.uearname.text=it.data.user.nickname
                idid= it.data.user.id.toString()
                binding.shuzi1.text=it.data.share_count.toString()
                binding.shuzi2.text=it.data.collection_count.toString()
                shoucangshu=it.data.collection_count
                dianzanshu=it.data.fav_count
                binding.shuzi3.text=it.data.comment_count.toString()
                binding.shuzi4.text=it.data.fav_count.toString()



            }else{
                Toast.makeText(this, it.msg, Toast.LENGTH_SHORT).show()
            }

        })




    }

    override fun onItemClick(position: Int, uri: Commentpl) {
        xiaobiao=position
        viewmodel.ondianzanplwang(UserData.token,mapOf("id" to uri.id.toString()))


    }
}